
package com.insigma.business.qrcode.soldier._provider.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.insigma.business.qrcode.common.dto.CommonAdminAreaDTO;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.MemCacheManager;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.BizException;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.intell.entity.UserDO;
import com.insigma.business.qrcode.intell.service.UserService;
import com.insigma.business.qrcode.soldier._consumer.HeroServiceConsumer;
import com.insigma.business.qrcode.soldier._provider.CompanyProvider;
import com.insigma.business.qrcode.soldier.dto.*;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.soldier.entity.*;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.service.*;
import com.insigma.business.qrcode.common.converter.CompanyProviderConverter;
import com.insigma.business.qrcode.common.converter.JobRecruitPositionProviderConverter;
import com.insigma.business.qrcode.soldier.util.ThreadPoolManager;
import com.insigma.business.qrcode.welfare.util.PasswordUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;


@Slf4j
@Service
@Component("companyProvider")
public class CompanyProviderImpl extends BaseProviderImpl implements CompanyProvider {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CompanyTypeService companyTypeService;

    @Autowired
    private CompanyEntryApplyService companyEntryApplyService;

    @Autowired
    private JobRecruitPositionService jobRecruitPositionService;

    @Autowired
    private JobFairParticipateService jobFairParticipateService;

    @Autowired
    private UserService userService;

    @Autowired
    private HeroServiceConsumer heroServiceConsumer;


    @Override
    public BasePagingResponse<List<CompanyDTO>> searchCompany(final CompanySearchRequest searchRequest) {

        final BasePagingResponse<List<CompanyDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页Company的请求参数不能为空");
            }

            @Override
            public void executeService() {
                if (Strings.isBlank(searchRequest.getSortKeyValueJoint())) {
                    searchRequest.setSortKeyValueJoint("createDatetime_desc");
                }

                final CompanyBSearcher companyBSearcher = CompanyProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CompanyDO> companyDOList = companyService.search(companyBSearcher);
                final List<CompanyDTO> companyDTOList = CompanyProviderConverter.convertResponseDOList2DTOList(companyDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, companyDTOList, searchRequest, companyBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BaseResponse<List<CompanyPositNoFairIdGroupDTO>> getCompanyPositNoFairId() {
        List<CompanyPositNoFairIdDO> dOlist = companyService.getCompanyPositNoFairId();
        if (CollectionUtils.isEmpty(dOlist)) {
            return ResponseUtils.buildSuccessResponse(new ArrayList<>());
        }
        List<CompanyPositNoFairIdGroupDTO> result = Lists.newArrayList();
        Map<CompanyPositNoFairIdGroupDTO, List<CompanyPositNoFairIdDTO>> map = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(dOlist)) {
            Map<String, List<CompanyPositNoFairIdDO>> groupByOrg = dOlist.stream()
                    .collect(Collectors.groupingBy(CompanyPositNoFairIdDO::getOrgId));
            for (Map.Entry<String, List<CompanyPositNoFairIdDO>> entry : groupByOrg.entrySet()) {
                CompanyPositNoFairIdGroupDTO dto = new CompanyPositNoFairIdGroupDTO();
                dto.setOrgId(entry.getKey());
                dto.setOrgName(entry.getValue().get(0).getOrgName());
                List<CompanyPositNoFairIdDTO> newlist = Lists.newArrayList();
                List<CompanyPositNoFairIdDO> oldlist = entry.getValue();
                for (CompanyPositNoFairIdDO noFairIdDO : oldlist) {
                    CompanyPositNoFairIdDTO noFairIdDTO = new CompanyPositNoFairIdDTO();
                    BeanUtils.copyProperties(noFairIdDO, noFairIdDTO);
                    newlist.add(noFairIdDTO);
                }
                dto.setPosition(newlist);
                result.add(dto);
            }
        }


        return ResponseUtils.buildSuccessResponse(result);

    }

    @Override
    public BasePagingResponse<List<CompanyDTO>> searchCompanyOfQualification(CompanySearchRequest companySearchRequest) {
        if (Strings.isBlank(companySearchRequest.getSortKeyValueJoint())) {
            companySearchRequest.setSortKeyValueJoint("c.createDatetime_desc");
        }
        final CompanyBSearcher companyBSearcher = CompanyProviderConverter.convertRequestSO2Searcher(companySearchRequest);
        final List<CompanyDO> companyDOList = companyService.searchByQualification(companyBSearcher);
        final List<CompanyDTO> companyDTOList = CompanyProviderConverter.convertResponseDOList2DTOList(companyDOList);
        return ResponseUtils.buildSuccessPagingResponse(companyDTOList, companyBSearcher);
    }


    @Override
    public BaseResponse<CompanyDTO> getCompany(final BaseGetRequest baseGetRequest) {

        final BaseResponse<CompanyDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条Company的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CompanyDO companyDO = companyService.get((String) baseGetRequest.getId());
                final CompanyDTO remoteObj = CompanyProviderConverter.convertResponseDO2DTO(companyDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<CompanyDTO> getCompanyWithJobPosition(BaseGetRequest baseGetRequest) {

        BizCheckUtil.checkNotNull(baseGetRequest, "获取单条Company的请求参数不能为空");
        BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");

        CompanyDO companyDO = companyService.get((String) baseGetRequest.getId());
        if (companyDO == null) {
            JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.getByOrgId((String) baseGetRequest.getId());
            if (jobFairParticipateDO != null) {
                companyDO = CompanyProviderConverter.convertJobFairParticipateDO2CompanyDO(jobFairParticipateDO);
                companyDO.setLegalAddress(companyDO.getAddress());
                companyDO.setRemark(companyDO.getDescription());
            }
        }
        BizCheckUtil.checkNotNull(companyDO, "公司不存在");
        final CompanyDTO remoteObj = CompanyProviderConverter.convertResponseDO2DTO(companyDO);
        List<JobRecruitPositionDO> jobRecruitPositionDOS = jobRecruitPositionService.listPositionsOfCompany((String) baseGetRequest.getId());
        final List<JobRecruitPositionDTO> jobRecruitPositionDTOList = JobRecruitPositionProviderConverter.convertResponseDOList2DTOList(jobRecruitPositionDOS);
        remoteObj.setPositions(jobRecruitPositionDTOList);
        return ResponseUtils.buildSuccessResponse(remoteObj);
    }

    @Override
    public BaseResponse<Integer> countCompany(CompanySearchRequest searchRequest) {

        BizCheckUtil.checkNotNull(searchRequest, "请求参数不能为空");

        int i = companyService.countByBetweenTime(searchRequest.getBeginDate(), searchRequest.getEndDate());
        return ResponseUtils.buildSuccessResponse(i);
    }

    @Override
    public BaseResponse<CompanyDTO> getCompanyByUserId(String userId) {

        BizCheckUtil.checkNotNull(userId, "获取单条Company的请求参数userId不能为空");

        final CompanyDO companyDO = companyService.getByUserId(userId);
        final CompanyDTO remoteObj = CompanyProviderConverter.convertResponseDO2DTO(companyDO);

        return ResponseUtils.buildSuccessResponse(remoteObj);
    }


    @Override
    public BaseResponse<CompanyDTO> getCompanyByEmployeeId(final String employeeId) {


        final BaseResponse<CompanyDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotBlank(employeeId, "用户id不能为空");
            }

            @Override
            public void executeService() {
                final CompanyDO companyDO = companyService.getByUserId(employeeId);
                final CompanyDTO remoteObj = CompanyProviderConverter.convertResponseDO2DTO(companyDO);

                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_GET_BY_USER_ID;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCompany(final CompanyInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增Company的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CompanyDO companyDO = CompanyProviderConverter.convertRequestIO2DO(insertRequest);
                companyService.insert(companyDO);
                ResponseUtils.setupSuccessResponse(baseResponse, companyDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> insertJobReportCompany(CompanyInsertRequest insertRequest) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增Company的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CompanyDO companyDO = CompanyProviderConverter.convertRequestIO2DO(insertRequest);

                companyService.insert(companyDO);
                ResponseUtils.setupSuccessResponse(baseResponse, companyDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCompany(final CompanyUpdateRequest updateRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改Company的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                CompanyDO companyDO = CompanyProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = companyService.update(companyDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> updateCompanyExtraInfo(CompanyUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "请求不能为空");
        BizCheckUtil.checkNotNull(updateRequest.getManagerId(), "userid请求不能为空");

        CompanyDO companyDO = companyService.getByUserId(updateRequest.getManagerId());
        BizCheckUtil.checkNotNull(companyDO, "公司不存在");
        if (Strings.isNotBlank(updateRequest.getUnitSize())) {
            companyDO.setUnitSize(updateRequest.getUnitSize());
        }
        if (Strings.isNotBlank(updateRequest.getOfficialWebsiteUrl())) {
            companyDO.setOfficialWebsiteUrl(updateRequest.getOfficialWebsiteUrl());
        }
        if (Strings.isNotBlank(updateRequest.getRemark())) {
            companyDO.setRemark(updateRequest.getRemark());
        }
        if (Strings.isNotBlank(updateRequest.getWorkingHours())) {
            companyDO.setWorkingHours(updateRequest.getWorkingHours());
        }
        if (Strings.isNotBlank(updateRequest.getOffHours())) {
            companyDO.setOffHours(updateRequest.getOffHours());
        }
        if (Strings.isNotBlank(updateRequest.getOvertime())) {
            companyDO.setOvertime(updateRequest.getOvertime());
        }
        if (Strings.isNotBlank(updateRequest.getBreakTime())) {
            companyDO.setBreakTime(updateRequest.getBreakTime());
        }
        if (Strings.isNotBlank(updateRequest.getOtherBenefits())) {
            companyDO.setOtherBenefits(updateRequest.getOtherBenefits());
        }
        if (Strings.isNotBlank(updateRequest.getImages())) {
            companyDO.setImages(updateRequest.getImages());
        }
        if (Strings.isNotBlank(updateRequest.getLogoRelativeUri())) {
            companyDO.setLogoRelativeUri(updateRequest.getLogoRelativeUri());
        }
        if (Strings.isNotBlank(updateRequest.getName())) {
            companyDO.setName(updateRequest.getName());
        }
        if (Strings.isNotBlank(updateRequest.getLogoRelativeUri())) {
            companyDO.setLogoRelativeUri(updateRequest.getLogoRelativeUri());
        }
        if (Strings.isNotBlank(updateRequest.getLegalUnifySocialCreditCode())) {
            companyDO.setLegalUnifySocialCreditCode(updateRequest.getLegalUnifySocialCreditCode());
        }
        if (updateRequest.getLegalRegisterCapital() != null) {
            companyDO.setLegalRegisterCapital(updateRequest.getLegalRegisterCapital());
        }
        if (Strings.isNotBlank(updateRequest.getLegalUnitType())) {
            companyDO.setLegalUnitType(updateRequest.getLegalUnitType());
        }
        if (updateRequest.getLegalEstablishDate() != null) {
            companyDO.setLegalEstablishDate(updateRequest.getLegalEstablishDate());
        }
        if (Strings.isNotBlank(updateRequest.getLegalPersonName())) {
            companyDO.setLegalPersonName(updateRequest.getLegalPersonName());
        }
        if (Strings.isNotBlank(updateRequest.getLegalAreaCode())) {
            companyDO.setLegalAreaCode(updateRequest.getLegalAreaCode());
        }
        if (Strings.isNotBlank(updateRequest.getLegalAddress())) {
            companyDO.setLegalAddress(updateRequest.getLegalAddress());
        }
        if (updateRequest.getLegalLatitude() != null) {
            companyDO.setLegalLatitude(updateRequest.getLegalLatitude());
        }
        if (updateRequest.getLegalLongitude() != null) {
            companyDO.setLegalLongitude(updateRequest.getLegalLongitude());
        }
        if (updateRequest.getIndustryClassify() != null) {
            companyDO.setIndustryClassify(updateRequest.getIndustryClassify());
        }
        if (updateRequest.getMilitaryEnterprise() != null) {
            companyDO.setMilitaryEnterprise(updateRequest.getMilitaryEnterprise());
        }
        if (updateRequest.getDemonstrationEnterprise() != null) {
            companyDO.setDemonstrationEnterprise(updateRequest.getDemonstrationEnterprise());
        }
        if (updateRequest.getHiredSoldierNum() != null) {
            companyDO.setHiredSoldierNum(updateRequest.getHiredSoldierNum());
        }
        if (updateRequest.getPioneerPark() != null) {
            companyDO.setPioneerPark(updateRequest.getPioneerPark());
        }
        if (Strings.isNotBlank(updateRequest.getUnitSize())) {
            companyDO.setUnitSize(updateRequest.getUnitSize());
        }
        if (Strings.isNotBlank(updateRequest.getContactNo())) {
            companyDO.setContactNo(updateRequest.getContactNo());
        }
        if (Strings.isNotBlank(updateRequest.getContactPerson())) {
            companyDO.setContactPerson(updateRequest.getContactPerson());
        }
        if (Strings.isNotBlank(updateRequest.getLandline())) {
            companyDO.setLandline(updateRequest.getLandline());
        }
        if (Strings.isNotBlank(updateRequest.getFileRelativeUriJoint())) {
            companyDO.setFileRelativeUriJoint(updateRequest.getFileRelativeUriJoint());
        }

        Integer update = companyService.update(companyDO);
        return ResponseUtils.buildSuccessResponse(update);
    }


    @Override
    public BaseResponse<Integer> deleteCompany(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除Company的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
            }

            @Override
            public void executeService() {
                final Integer affectRowNum = companyService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMPANY_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Map<String, String>> getCompanyId2NameMapByIdList(final List<String> idList) {


        final BaseResponse<Map<String, String>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkIsTrue(CollectionUtil.isNotEmpty(idList), "企业id集合不能为空");
            }

            @Override
            public void executeService() {
                final List<KeyValueCDO> keyValueCDOList = companyService.listId2ValueByIdList(idList);
                if (CollectionUtil.isNotEmpty(keyValueCDOList)) {
                    final Map<String, String> companyId2NameMap = new HashMap<>(keyValueCDOList.size());
                    for (KeyValueCDO keyValueCDO : keyValueCDOList) {
                        companyId2NameMap.put(keyValueCDO.getKey(), keyValueCDO.getValue());
                    }

                    ResponseUtils.setupSuccessResponse(baseResponse, companyId2NameMap);
                } else {
                    ResponseUtils.setupSuccessResponse(baseResponse, Collections.emptyMap());
                }
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.GET_ID_NAME_MAPPING_BY_ID_LIST;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> importCompany(SimpleApiRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getUrl(), "url不能为空");

        String url = request.getUrl();
        url = url.replace("115.227.32.11", "172.18.102.62");
        try {
            URL source = new URL(url);
            InputStream inputStream = source.openStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream);

            final List<List<Object>> read = reader.read();


            List<Object> objects = read.get(0);
            checkSoldierImportTemplate(objects, "序号", "备注", "HR手机号", "公司名称", "信用代码", "注册资本", "企业类型", "法人", "地区", "地址", "行业类型", "经济类型", "公司规模", "联系人", "公司介绍");

            ThreadPoolManager.getInstance().submit(new Runnable() {
                @Override
                public void run() {
                    List<String> error = Lists.newArrayList();
                    for (int i = 1; i < read.size(); i++) {
                        try {
                            List<Object> item = read.get(i);
                            String tyj = (String) item.get(1);
                            String mobile = item.get(2).toString();
                            String name = (String) item.get(3);
                            String certNo = (String) item.get(4);

                            Long money = 1000000L;
                            Object o = item.get(5);
                            if (o instanceof Long) {
                                money = (Long) o;
                            }
                            String companyType = (String) item.get(6);
                            String faRen = (String) item.get(7);
                            String area = (String) item.get(8);
                            String address = (String) item.get(9);
                            String industryType = (String) item.get(10);
                            String jjType = (String) item.get(11);
                            String unitSize = (String) item.get(12);
                            String person = (String) item.get(13);
                            String info = (String) item.get(14);

                            CompanyDO byCompanyName = companyService.getByCompanyName(name);
                            BizCheckUtil.checkIsTrue(byCompanyName == null, "已存在");

                            UserDO userDO = new UserDO();
                            userDO.setAccountName(mobile);
                            userDO.setPassword(PasswordUtils.encode(mobile, mobile.substring(mobile.length() - 6)));
                            userDO.setName(person);
                            userDO.setMobile(mobile);
                            userDO.setType("COMPANY_USER");
                            userDO.setStatus("ENABLED");
                            userService.insert(userDO);

                            CompanyDO companyDO = new CompanyDO();
                            companyDO.setManagerId(userDO.getId());
                            companyDO.setName(name);
                            companyDO.setLegalUnifySocialCreditCode(certNo);
                            companyDO.setLegalRegisterCapital(money);
                            companyDO.setLegalUnitType(companyType);
                            companyDO.setLegalPersonName(faRen);
                            companyDO.setLegalAddress(address);
                            companyDO.setLegalAreaCode(heroServiceConsumer.analyseAreaCodeByBasicInfoForOther(area));
                            companyDO.setIndustryClassify(industryType);
                            companyDO.setEconomicType(jjType);
                            companyDO.setContactPerson(person);
                            companyDO.setContactNo(mobile);
                            companyDO.setRemark(info);
                            companyDO.setUnitSize(buildUnitSize(unitSize));
                            companyService.insert(companyDO);

                            CompanyEntryApplyDO companyEntryApplyDO = new CompanyEntryApplyDO();
                            companyEntryApplyDO.setName(name);
                            companyEntryApplyDO.setLegalUnifySocialCreditCode(certNo);
                            companyEntryApplyDO.setLegalRegisterCapital(money);
                            companyEntryApplyDO.setLegalUnitType(companyType);
                            companyEntryApplyDO.setLegalPersonName(faRen);
                            companyEntryApplyDO.setLegalAddress(address);
                            companyEntryApplyDO.setLegalAreaCode(heroServiceConsumer.analyseAreaCodeByBasicInfoForOther(area));
                            companyEntryApplyDO.setIndustryClassify(industryType);
                            companyEntryApplyDO.setEconomicType(jjType);
                            companyEntryApplyDO.setContactPerson(person);
                            companyEntryApplyDO.setContactNo(mobile);
                            companyEntryApplyDO.setRemark(info);
                            companyEntryApplyDO.setUnitSize(buildUnitSize(unitSize));
                            String guideUnitCode = heroServiceConsumer.analyseAreaCodeByBasicInfoForOther(tyj);
                            CommonAdminAreaDTO commonAdminArea = heroServiceConsumer.getCommonAdminArea(guideUnitCode);
                            companyEntryApplyDO.setGuideUnitCode(guideUnitCode);
                            companyEntryApplyDO.setGuideUnit(commonAdminArea.getValue() + "退役局");
                            companyEntryApplyDO.setAuditStatus("SUCCESS");
                            companyEntryApplyDO.setApplicantId(userDO.getId());
                            companyEntryApplyDO.setCompanyId(companyDO.getId());
                            companyEntryApplyService.insert(companyEntryApplyDO);

                            CompanyTypeDO companyTypeDO = new CompanyTypeDO();
                            companyTypeDO.setCompanyId(companyDO.getId());
                            companyTypeDO.setCompanyBizType("RECRUIT");
                            companyTypeService.insert(companyTypeDO);

                            CompanyTypeDO companyTypeDO2 = new CompanyTypeDO();
                            companyTypeDO2.setCompanyId(companyDO.getId());
                            companyTypeDO2.setCompanyBizType("COMMON");
                            companyTypeService.insert(companyTypeDO2);
                        } catch (Exception e) {
                            log.error("导入错误：{}", e);

                        }
                    }
                }
            });


        } catch (BizException e) {
            log.error("导入错误：{}", e);
            throw e;
        } catch (Exception e) {
            log.error("导入错误：{}", e);
            throw new BizException(ErrorCodeEnum.ILLEGAL_PARAM);
        }

        return ResponseUtils.buildSuccessResponse(0);
    }

    @Override
    public BaseResponse<CompanyTrainingStatsDTO> statsCompanyTraining(CompanySearchRequest request) {

        CompanyTrainingStatsDTO dto = new CompanyTrainingStatsDTO();
        dto.setClassNum(0);
        dto.setStudentNum(0);
        dto.setCommentNum(0);

        return ResponseUtils.buildSuccessResponse(dto);
    }

    @Override
    public BaseResponse<CompanyTrainingStatsDTO> statsCompanyTrainingOfGov(CompanySearchRequest request) {


        return null;
    }

    @Override
    public CompanyDTO getCachedCompanyInfo(String companyId) {
        try {
            Object o = MemCacheManager.getInstance().get("JOB_COMPANY_INFO_" + companyId, new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    CompanyDTO dto = getCompany(new BaseGetRequest(companyId)).getDto();
                    if (dto == null) {
                        return new CompanyDTO();
                    }
                    return dto;
                }
            });
            return (CompanyDTO)o;
        } catch (ExecutionException e) {
            log.error("读取异常：{}", e);
        }
        return null;
    }

    private String buildUnitSize(String unitSize) {
        unitSize = unitSize.replace("-", ",");
        if (unitSize.equals("10000,")) {
            unitSize = unitSize + "∞";
        }
        return "[" + unitSize + "]";
    }

    private void checkSoldierImportTemplate(List<Object> headers, String... names) {
        for (int i = 0; i < names.length; i++) {
            BizCheckUtil.checkIsTrue(((String) headers.get(i)).startsWith(names[i]), "模板错误");
        }
    }

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date endDate = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH, -7);
        Date beginDate = cal.getTime();
        System.out.println(beginDate);
        System.out.println(endDate);
    }
}
